package tum0r.server.problem.web;

import tum0r.model.ProblemInfoItem;
import tum0r.model.ProblemLevel;
import tum0r.server.problem.ProblemBase;
import tum0r.webengine.annotation.Server;
import tum0r.webengine.utils.server.action.Action;

import java.util.LinkedHashMap;

/**
 * 工程: Security<br>
 * 包: tum0r.server.problem.web.sql_injection<br>
 * 创建者: tum0r<br>
 * 创建时间: 2021/6/21 23:15<br>
 * <br>
 */
@Server(Mapping = "/security/problem/web/LocalAccess")
public class LocalAccess extends ProblemBase {
    public LocalAccess() {
        information.ProblemTitle = "有趣的127.0.0.1";
        information.ProblemID = 10;
        information.Creator = "tum0r";
        information.ProblemLevel = ProblemLevel.FUNNY;
        information.CreateTime = "2020/06/21";

        ProblemInfoItem problem = new ProblemInfoItem("problem", null);
        information.ProblemInfo.add(problem);
    }

    @Override
    public void description(Action<String> action) {
        action.callBack("你能从127.0.0.1访问problem吗");
    }

    @Override
    public void tips(Action<String> action) {
        action.callBack("XFF: 127");
    }

    @Override
    public void writeUp(Action<String> action) {
        action.callBack("""
                构造HTTP请求头 X-Forwarded-For: 127.0.0.1 访问problem即可
                """);
    }

    public void problem(Action<String> action) {
        LinkedHashMap<String, String> headers = action.getRequestHeaders();
        String result = null;
        if (!headers.containsKey("X-Forwarded-For")) {
            result = "你的IP地址是：" + action.getConnectiveAddress().getHostAddress();
        } else {
            String xff = headers.get("X-Forwarded-For");
            if ("127.0.0.1".equals(xff)) {
                result = """
                        There's no place like 127.0.0.1
                                                
                        wectf{L0c@l_Acc3s5_Is_xFF}
                        """;
            } else {
                result = "你的IP地址是：" + xff;
            }
        }

        action.callBack(result);
    }
}
